//
//  CusTableViewCell.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/26.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "CusTableViewCell.h"
#import "ChartTableCell.h"
#import "NSString+StringSize.h"

@interface CusTableViewCell()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *listTableView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *label1;
@property (nonatomic,strong)UILabel *label2;
@property (nonatomic,strong)UILabel *label3;
@property (nonatomic,strong)NSArray *listArr;
@property (nonatomic,strong)UILabel *centerLabel;

@end
static NSString *ChartTableCellID = @"ChartTableCellID";
@implementation CusTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _listArr = [NSArray new];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    _listArr = [NSArray array];
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    
 
    
    _titleLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:@"行驶里程排行前五名"];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(31);
        make.centerY.mas_equalTo(self.mas_top).offset(134.0/3);
    }];
 
    
    UIView *backselView = [UIView new];
    backselView.backgroundColor = [UIColor colorWithRed:0.93f green:0.96f blue:1.00f alpha:1.00f];
    [self addSubview:backselView];
    
    _label1= [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:@"排名"];
    [backselView addSubview:_label1];
    [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(31);
        make.centerY.mas_equalTo(self.mas_top).offset(276.f/3);
    }];
    _label2= [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:@"里程数(km)"];
    [backselView addSubview:_label2];
    [_label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).offset(-kScreenWidth*0.05);
        make.centerY.mas_equalTo(_label1);
    }];
    _label3= [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:@"排名"];
    [backselView addSubview:_label3];
    [_label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-31);
        make.centerY.mas_equalTo(_label1);
    }];
    
    
    
    [backselView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_label3.mas_centerY);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(42);
    }];
    
    _listTableView = [UITableView new];
    [self addSubview:_listTableView];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    _listTableView.bounces = NO;
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_listTableView registerClass:[ChartTableCell class] forCellReuseIdentifier:ChartTableCellID];
    [_listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.top.mas_equalTo(self.mas_top).offset(340.f/3);
    }];
    
    _centerLabel = [UILabel LabelWithFont:BigFontSize andColor:TitleColor andTextAlignment:center andString:@"无统计数据"];
    _centerLabel.hidden = YES;
    [self addSubview:_centerLabel];
    [_centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self);
    }];
    
}
- (void)laodTableWithDataArr:(NSArray *)arr
{
    _listArr = arr;
    [_listTableView reloadData];
    if (_listArr.count == 0) {
        _centerLabel.hidden = NO;
    }else{
        _centerLabel.hidden = YES;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_listArr.count) {
        return _listArr.count;
    }else{
        return 0;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChartTableCell *chartCell = [tableView dequeueReusableCellWithIdentifier:ChartTableCellID];
    if (_listArr.count) {
        ChartTableModel *tempModel = _listArr[indexPath.row];
        chartCell.model = tempModel;
        CGSize size1 = [_leftString sizeWithLabelWidth:300 font:[UIFont systemFontOfSize:NormalFontSize]];
        chartCell.length1 = size1.width;
        CGSize size2 = [_rightString sizeWithLabelWidth:300 font:[UIFont systemFontOfSize:NormalFontSize]];
        chartCell.length2 = size2.width;
    
        Kweaself
        chartCell.backReloadUI = ^(NSString *length) {
            [_label2 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(weakSelf.mas_centerX).offset(-kScreenWidth*0.1);
                make.centerY.mas_equalTo(_label1);
            }];
            _label3.textAlignment = NSTextAlignmentCenter;
            [_label3 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo([length floatValue]);
                make.right.mas_equalTo(self.mas_right).offset(-31);
                make.centerY.mas_equalTo(_label1);
            }];
        };
    }
    return chartCell;
}
- (void)layoutSubviews
{
    _titleLabel.text = _title;
    _label1.text = _leftString;
    _label2.text = _centerString;
    _label3.text = _rightString;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125.f/3;
}


@end
